package com.ym.b2b.tags;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.ym.b2b.dictionary.dao.EmSDictionaryDAO;
import com.ym.b2b.dictionary.sqlbean.EmSDictionary;
import com.ym.b2b.tags.base.BaseTag;
import com.zc.commons.base.Result;
import com.zc.commons.spring.SpringContextHolder;

public class KeyValueTags extends BaseTag {

    private static final long serialVersionUID = 895777383599438520L;

    protected String          key;
    protected String		  type;

    public int doStartTag() {

        // 组装key 组
        key = key.replace(",", "','");
        EmSDictionaryDAO emSDictionaryDAO = SpringContextHolder.getBean("emSDictionaryDAO");
        List<EmSDictionary> e = null;
        if(type != null &"xc".equals(type)){
        	e= emSDictionaryDAO.showEmSDictionaryByKeyAndType("'%" + key + "%'");
        	 Result ret = new Result();
        	 ret.setRows(e);
        	 getPageContext().getRequest().setAttribute(getOutResponseAttrName(), ret);
        	 return EVAL_BODY_INCLUDE;
        }else{
        	e = emSDictionaryDAO.showEmSDictionaryByKey("'" + key + "'");
        }

        Map<String, Object> map = new HashMap<String, Object>();
        if (CollectionUtils.isNotEmpty(e)) {
            for (EmSDictionary f : e) {
                map.put(f.getKey(), f);
            }
        }
        System.out.println(JSON.toJSONString(map));
        getPageContext().getRequest().setAttribute(getOutResponseAttrName(), map);
        return EVAL_BODY_INCLUDE;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
